<?php

namespace app\admin\model\outhouse;


use think\Model;
use traits\model\SoftDelete;

class CheckVouch extends Model
{

    //use SoftDelete;

    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 表名
    protected $name = 'check_vouch';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';

    // 追加属性
    protected $append = [
        'type_text',
        'shelves_text',
        'process_text'
        //'status_text'
    ];
    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3'), '4' => __('Type 4')];
    }
    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }
    
    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    
    public function getShelvesTextAttr($value, $data)
    {
        $str = '';
        if(isset($data['shelves_ids'])){
            $shelves = db('shelves')->where('id','in',$data['shelves_ids'])->column('name','id');
            $str = implode('|', $shelves);
        }
        return $str;
    }
    
    public function getProcessTextAttr($value, $data)
    {
        $str = '';
        $count1 = db('check_vouch_list')->where('code',$data['code'])->count();
        $count2 = db('check_vouch_list')->where([
            'code'=>$data['code'],
            'scan_flag'=>1
        ])
        ->count();
        
        if($count1){
            $str = round($count2/$count1,2)*100;
        }
        
        if($str>=100){
            //进度完成 先不自动关闭
            //self::where('id',$data['id'])->update(['status'=>2]);
        }
        
        return $str.'%';
    }

    public static  function  addData($params){
        $tpdjLog = [
            'code'=>$params['code'],
            'book_date'=>$params['book_date'],
            'code_date' => $params['code_date'],
            'warehouse_id' => $params['warehouse_id'],
            'warehouse_code' => $params['warehouse_code'],
            'createtime'=>date('Y-m-d H:i:s'),
            //'updatetime'=>date('Y-m-d H:i:s'),
            'remark' => isset($params['remark'])?$params['remark']:''
        ];
        self::insert($tpdjLog);
        return self::getLastInsID();

    }
    public function wareHouse(){
        return $this->hasOne('\app\admin\model\information\Warehouse','id','warehouse_id')->field('id,name');
    }





}
